<%-- 
    Document   : addOrEditGoods
    Created on : Oct 27, 2011, 7:52:54 AM
    Author     : Admin
--%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.Collection"%>
<%@page import="javax.rmi.PortableRemoteObject"%>
<%@page import="manufacturerBean.ManufacturerHome"%>
<%@page import="manufacturerBean.Manufacturer"%>
<%@page import="categorybean.Category"%>
<%@page import="categorybean.CategoryHome"%>
<%@page import="GoodsBean.Goods"%>
<%@page import="GoodsBean.GoodsHome"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="java.util.Set"%>
<%@page import="java.util.HashMap"%>
<%@page import="DBConnection.ATeamObject"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Добавление товара</title>
        <%  InitialContext initialContext;
            GoodsHome goodsHome;
            Goods goods;     
            Object homeObject;
            
            CategoryHome categoryHome;
            Category category;
            Object homeObject1;
            
            Manufacturer manufacturer;
            ManufacturerHome manufacturerHome;
            Object homeObject2;
                 
            try{
                initialContext = new InitialContext();
                homeObject = initialContext.lookup("GoodsBean");
                goodsHome = (GoodsHome) PortableRemoteObject.narrow(homeObject,
                        GoodsHome.class);
                
                homeObject1 = initialContext.lookup("CategoryBean");
                categoryHome = (CategoryHome) PortableRemoteObject.narrow(homeObject1,
                        CategoryHome.class);
                
                homeObject2 = initialContext.lookup("ManufacturerBean");
                manufacturerHome = (ManufacturerHome) PortableRemoteObject.narrow(homeObject2,
                        ManufacturerHome.class);
        %>
    </head>
    <body background="1.jpg" bgproperties="fixed">
        <h4><a href="index.jsp">Главная</a></h4> 
                <a href="addOrEditGoods.jsp">Добавление товара</a>
        <h1>Добавление товара</h1>
        <%  if (request.getParameter("category") != null){
                ATeamObject atoForNewGood = new ATeamObject();
                atoForNewGood.setName(request.getParameter("name"));                
                atoForNewGood.setObjectDescription(request.getParameter("description"));
                
                category = categoryHome.findByPrimaryKey(Integer.parseInt(request.getParameter("category")));
                atoForNewGood.setParentId(category.getObjectId());
                
                manufacturer = manufacturerHome.findByPrimaryKey(Integer.parseInt(request.getParameter("manufacturer")));
                atoForNewGood.setManifacturerId(manufacturer.getObjectId());
                
                atoForNewGood.setPrice(Integer.parseInt(request.getParameter("price")));
                atoForNewGood.setNumber(Integer.parseInt(request.getParameter("value")));
                
                goods = goodsHome.create(atoForNewGood);
                
                response.sendRedirect("index.jsp");                
            }
        %>
        <form name="addGoods" action="addOrEditGoods.jsp">
            Введите название товара
            <input type="text" name="name" value="" size="50" />
            Введите описание товара
            <input type="text" name="description" value="" size="50" />
            <p> Категория товара
                <select name="category">
                <%  Collection allCategories = categoryHome.findAll();
                    Iterator iter = allCategories.iterator();
                    while(iter.hasNext()){
                        category = (Category)iter.next();
                %>
                        <option value="<%= category.getObjectId() %>"><%= category.getName() %></option>
                <%
                    };
                %>
                </select>
            </p>    
            <p> Введите производителя
                <select name="manufacturer">
                <%  Collection allManufacturers = manufacturerHome.findAll();
                    iter = allManufacturers.iterator();
                    while(iter.hasNext()){
                        manufacturer = (Manufacturer)iter.next();
                %>
                        <option value="<%= manufacturer.getObjectId() %>"><%= manufacturer.getName() %></option>
                <%
                    };
                %>
                </select>
            </p>
            <p> Введите цену
                <input type="text" name="price" value="" size="25" />
            </p>
            <p> Введите количество
                <input type="text" name="value" value="" size="25" />
            </p>
            <p>
                <input type="submit" value="Добавить" name="addToDB" />
            </p>
        </form>
    <% } catch(Exception e){
            e.printStackTrace(System.err);
       }
    %>
    </body>
</html>
